package com.stms.service.impl;

import com.stms.dao.StaffRepository;
import com.stms.pojo.Contract;
import com.stms.pojo.Staff;
import com.stms.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    public StaffRepository staffRepository;

    @Override
    public boolean addStaff(Staff staff) {
        if(staffRepository.findByIdcard(staff.getIdcard())!=null){
            return false;
        }else{
            if(staffRepository.save(staff)!=null) {
                return true;
            }else{
                return false;
            }
        }
    }

    @Override
    public boolean editStaff(Staff staff) {
        if(staffRepository.save(staff)!=null){
            return true;
        }else{
            return false;
        }
    }


    @Override
    public void deleteBatch(String staffIds) {
        String[] staffStr = staffIds.split(",");
        List<Integer> list = new ArrayList<Integer>();

        for(String str:staffStr){
            list.add(Integer.valueOf(str));
        }
        staffRepository.deleteBatch(list);
    }

    @Override
    public Page<Staff> fuzzyQuery(Integer pageNum, Integer pageSize, String direction, Staff staff) {
        if(pageNum==null||pageNum==0){
            pageNum = 1;
        }
        pageNum = pageNum-1;
        if(pageSize==null){
            pageSize = 10;
        }
        if(direction==null){
            direction = new String("DESC");
        }

        //排序、分页初始化
        Sort.Direction direc = direction.equals("DESC")?Sort.Direction.DESC:Sort.Direction.ASC;
        Pageable pageable = PageRequest.of(pageNum,pageSize, direc,"id");

        Page<Staff> page = staffRepository.findAll(new Specification<Staff>() {
            @Override
            public Predicate toPredicate(Root<Staff> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //创建条件集合
                List<Predicate> list = new ArrayList<>();
                if (staff == null) {
                    return null;
                }
                if (staff.getId() != null && !staff.getId().equals("")) {
                    list.add(cb.equal(root.get("id"), staff.getId()));
                }
                if (staff.getName() != null && !staff.getName().equals("")) {
                    list.add(cb.like(root.get("name").as(String.class), "%" + staff.getName().trim() + "%"));
                }
                Predicate[] predicate = new Predicate[list.size()];
                return cb.and(list.toArray(predicate));
            }
        }, pageable);
        return page;
    }

    @Override
    public Page<Staff> findByPage(Integer pageNum, Integer pageSize) {
        if(pageNum==null||pageNum==0){
            pageNum = 1;
        }
        pageNum = pageNum-1;
        if(pageSize==null){
            pageSize = 10;
        }

        PageRequest of = PageRequest.of(pageNum,pageSize);
        Page<Staff> page = staffRepository.findAll(of);
        return page;
    }
}
